   import java.io.*;
/**
 * This class is called to create an ics file when Event is validated and user pressed save.  
 * @author ICS 414 - Upgrade Project
 */
   public class ICSWriter{
   /**
    * Single static method is called on the class with Event Object passed as a parameter
    * @param e Event passed when inputs are validated and save button is pressed
    */
      public static void writeMe(Event e){
      
         String fileName = e.getName();
      
         String timeZone = e.getTimeZone();
         String tzOffset = e.icsOffset();
         String dtStart = e.icsStart();
         String dtEnd = e.icsEnd();
         String dtNow = e.icsNow();
         String classification = e.getClassification();
         String path = e.getPath();
         String comment = e.getComment();
         String resources = e.getResources();
         String location = e.getLocation();
         String recurring = e.icsRecurring();
         String typeRecurring = e.getTypeRecur();
         String endDateRecurring = e.icsEndRecur();
         String directoryName = e.getDirectory();
      
      
         PrintWriter fileOut = null;
      
      
         File d = new File(path + directoryName);
         try{
            if(d.isDirectory()){
               System.out.println("Directory Already Created");
            }
            else if(d.mkdir()){
               System.out.println("Directory Created");
            }
            else{
               System.out.println("Directory is not created");
            }
         }
            catch(Exception g){
               g.printStackTrace();
            }
         try{
         //Previous setup was to hard code the save location as shown below
         //File f = new File("\\Users\\Micah Hwang\\Desktop\\" + fileName + ".ics");
         
         //new is to get path chosen and entered by user so no matter who the user is.
         //they can save to their own chosen location.
         //this is a feature thats needed because there is no "default" location for all users
         //It used to save in the source folder which may not be accessible to the user
            File f = new File(path + directoryName + fileName + ".ics");
            fileOut = new PrintWriter(f);
         }
            catch (FileNotFoundException exception){
               System.out.println("Error When creating File.");
            }
      		
         fileOut.println("BEGIN:VCALENDAR");
         fileOut.println("PRODID:-//ICS414");
         fileOut.println("VERSION:2.0");
         fileOut.println("CALSCALE:GREGORIAN");
         fileOut.println("METHOD:PUBLISH");
         fileOut.println("BEGIN:VTIMEZONE");
         fileOut.println("TZID:" + timeZone);
         fileOut.println("BEGIN:STANDARD");
         fileOut.println("TZOFFSETFROM:" + tzOffset); 
         fileOut.println("TZOFFSETTO:" + tzOffset);
         fileOut.println("TZNAME:" + timeZone);
         fileOut.println("DTSTART:19700101T000000");
         fileOut.println("END:STANDARD");
         fileOut.println("END:VTIMEZONE");
         fileOut.println("BEGIN:VEVENT");
         fileOut.println("DTSTART:" + dtStart); 
         fileOut.println("DTEND:" + dtEnd);
         fileOut.println("RRULE:FREQ=" + typeRecurring 
            			+ ";UNTIL=" + endDateRecurring);
         fileOut.println("DTSTAMP:" + dtNow);
         fileOut.println("CLASS:" + classification);
         fileOut.println("CREATED:" + dtNow);
         fileOut.println("DESCRIPTION:");
         fileOut.println("COMMENT:" + comment);
         fileOut.println("RESOURCES:" + resources);
         fileOut.println("LAST-MODIFIED:" + dtNow);
         fileOut.println("LOCATION:" + location);
         fileOut.println("SEQUENCE:2");
         fileOut.println("STATUS:CONFIRMED");
         fileOut.println("SUMMARY:" + fileName);
         fileOut.println("TRANSP:TRANSPARENT");
         fileOut.println("END:VEVENT");
         fileOut.println("END:VCALENDAR");
      
         fileOut.close();
      }
   }